import pandas as pd
from selenium import webdriver
import time
import re
import csv
import os
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By


# service = Service('./chromedriver.exe')
# browser = webdriver.Chrome(service=service)
# option = webdriver.ChromeOptions()
# browser.get('https://www.maoyan.com/')

def startBrowser():
    service = Service('./chromedriver.exe')
    option = webdriver.ChromeOptions()
    option.add_experimental_option('debuggerAddress','localhost:9223')
    browser = webdriver.Chrome(service=service,options=option)
    # browser.get('https://www.maoyan.com/')
    return browser

def init():
    if not os.path.exists('./movieList.csv'):
        with open('./movieList.csv','a',newline='',encoding='utf-8') as wf:
            write = csv.writer(wf)
            write.writerow(['title','movieImg','type','country','duration','releaseTime','rate','summary','director','actors','firstBoxOffice','allBoxOffice','detailUrl'])


def save_to_csv(resultData):
    with open('./movieList.csv', 'a', newline='', encoding='utf-8') as wf:
        write = csv.writer(wf)
        write.writerow(resultData)

def spider_out(detail):
    print('详情链接:'+detail)
    browser = startBrowser()
    browser.get(detail)
    browser.implicitly_wait(10)
    try:
        title = browser.find_element(by=By.XPATH, value='//div[@class="movie-brief-container"]/h1').text
        movieImg = browser.find_element(by=By.XPATH, value='//div[@class="avatar-shadow"]/img').get_attribute('src')
        type = browser.find_element(by=By.XPATH, value='//li[@class="ellipsis"][1]').text
        type = type.replace(' ', '-')
        cdString = browser.find_element(by=By.XPATH, value='//li[@class="ellipsis"][2]').text
        cdString = cdString.split('/')
        country = cdString[0].strip()
        try:
            duration = cdString[1].strip()
            matchD = re.search(r'\d+', duration)
            duration = matchD.group(0)
        except:
            duration = 0
        releaseTime = browser.find_element(by=By.XPATH, value='//li[@class="ellipsis"][3]').text
        releaseTime = releaseTime[:10]

        try:
            rate = browser.find_element(by=By.XPATH, value='//div[@class="index-right"]/div/div').get_attribute('style')
            match = re.search(r'\d+', rate)
            rate = match.group(0)
        except:
            rate = 0

        summary = browser.find_element(by=By.XPATH, value='//div[@class="mod-content"]/span').text
        director = browser.find_element(by=By.XPATH,
                                        value='//div[@class="celebrity-group"]/ul/li/div[@class="info"]/div').text
        actorsList = browser.find_elements(by=By.XPATH, value='//div[@class="celebrity-group"][2]/ul/li')
        aList = []
        for actor in actorsList:
            a = actor.find_element(by=By.XPATH, value='./div[@class="info"]/div[1]').text
            aList.append(a)
        aList = [name for name in aList if name]
        actors = '-'.join(aList)
        try:
            allBoxOffice = browser.find_element(by=By.XPATH, value='//div[@class="film-mbox"]/div[3]/div[1]').text
            try:
                firstBoxOffice = browser.find_element(by=By.XPATH, value='//div[@class="film-mbox"]/div[2]/div[1]').text
                if firstBoxOffice == '暂无':
                    firstBoxOffice = 0

            except:
                firstBoxOffice = 0

            try:
                allBoxOffice = browser.find_element(by=By.XPATH, value='//div[@class="film-mbox"]/div[3]/div[1]').text
                if firstBoxOffice == '暂无':
                    firstBoxOffice = 0
            except:
                allBoxOffice = 0
        except:
            try:
                firstBoxOffice = browser.find_element(by=By.XPATH, value='//div[@class="film-mbox"]/div[1]/div[1]').text
                if firstBoxOffice == '暂无':
                    firstBoxOffice = 0
            except:
                firstBoxOffice = 0

            try:
                allBoxOffice = browser.find_element(by=By.XPATH, value='//div[@class="film-mbox"]/div[2]/div[1]').text
                if allBoxOffice == '暂无':
                    allBoxOffice = 0
            except:
                allBoxOffice = 0
        detailUrl = detail

        print(title, movieImg, type, country, duration, releaseTime, rate, summary, director, actors, firstBoxOffice,
              allBoxOffice, detailUrl)
        save_to_csv(
            [title, movieImg, type, country, duration, releaseTime, rate, summary, director, actors, firstBoxOffice,
             allBoxOffice, detailUrl])
        time.sleep(1)
    except:
        pass


if __name__ == '__main__':
    init()
    # main()
    df = pd.read_csv('movieUrl.csv')
    column_0 = df.iloc[:,0].tolist()
    print(column_0)
    for detail in column_0:
        spider_out(detail)
        # break